package com.chihihx.launcher.ui.fragment

import android.R.id.tabs
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.HORIZONTAL
import com.chihihx.launcher.BaseWallPaperFragment
import com.chihihx.launcher.MainActivity
import com.chihihx.launcher.bean.Data
import com.chihihx.launcher.bean.HomeTabDto
import com.chihihx.launcher.bean.Movy
import com.chihihx.launcher.ext.getDrawableByName
import com.chihihx.launcher.ext.getHomeJsonDto
import com.chihihx.launcher.ui.dialog.MainTopDialog
import com.chihihx.launcher.ui.viewmodel.MainViewModel
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.databinding.FragmentMainHxtvBinding
import com.nova.android.tvlauncher.databinding.FragmentSplashHxtvBinding
import com.nova.android.tvlauncher.databinding.ItemHomeContentHorHxtvBinding
import com.nova.android.tvlauncher.databinding.ItemHomeContentVerHxtvBinding
import com.nova.android.tvlauncher.databinding.ItemHomeHeaderHxtvBinding
import com.nova.android.tvlauncher.databinding.ItemHomeIconBinding
import com.nova.android.tvlauncher.databinding.ItemHomeTabBinding
import com.nova.android.tvlauncher.ui.dialog.base.showDialog
import com_github_lib_base.base.BaseViewModel
import com_github_lib_base.ext.replaceChildFragment
import com_github_lib_base.ext.rv.addModels
import com_github_lib_base.ext.rv.models
import com_github_lib_base.ext.rv.setup
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainFragment : BaseWallPaperFragment<FragmentMainHxtvBinding, MainViewModel>() {

    override fun initView() {

        lifecycleScope.launch {
            delay(2500)
            showDialog(MainTopDialog()) {

            }
            mViewModel.getHomeInfo { }
        }

        mBind.rvIcon.layoutManager = LinearLayoutManager(requireActivity(), HORIZONTAL, false)
        mBind.rvIcon.setup(
            mutableListOf(
                R.drawable.icon_language,
                R.drawable.icon_language,
                R.drawable.icon_language,
                R.drawable.icon_language,
                R.drawable.icon_language,
                R.drawable.icon_language
            )
        ) {
            addType(Int::class, R.layout.item_home_icon)
            onBind {

                val itemBinding = getBinding<ItemHomeIconBinding>()
                val item = getModel<Int>()
                itemBinding.ivIcon.setImageResource(item)
            }
        }

        mBind.rvMain.setup(mutableListOf()) {
            addType(Movy::class, R.layout.item_home_header_hxtv)
            onBind {

                val itemBinding = getBinding<ItemHomeHeaderHxtvBinding>()
                val item = getModel<Movy>()
                itemBinding.ivHeader.setImageDrawable(item.icon?.getDrawableByName())

                itemView.setOnFocusChangeListener { v, hasFocus ->
                    if(hasFocus){
                        when(item.type){
                            0->{
                                mBind.rvContentVer.isVisible = true
                                mBind.rvContentHor.isVisible = false
                                item.datas?.let { mBind.rvContentVer.models = it.toMutableList() }
                            }
                            1->{
                                mBind.rvContentVer.isVisible = false
                                mBind.rvContentHor.isVisible = true
                                item.datas?.let { mBind.rvContentHor.models = it.toMutableList() }
                            }
                        }

                    }
                }

            }
        }

        mBind.rvContentHor.setup(mutableListOf()) {
            addType(Data::class, R.layout.item_home_content_hor_hxtv)
            onBind {

                val itemBinding = getBinding<ItemHomeContentHorHxtvBinding>()
                val item = getModel<Data>()
                itemBinding.ivContentHor.setImageDrawable(item.imageUrl?.getDrawableByName())

            }
        }

        mBind.rvContentVer.setNumColumns(5)
        mBind.rvContentVer.setup(mutableListOf()) {
            addType(Data::class, R.layout.item_home_content_ver_hxtv)
            onBind {

                val itemBinding = getBinding<ItemHomeContentVerHxtvBinding>()
                val item = getModel<Data>()
                itemBinding.ivContentVer.setImageDrawable(item.imageUrl?.getDrawableByName())

            }
        }

        val list = getHomeJsonDto().movies
        list?.let { mBind.rvMain.addModels(it) }

        mBind.rvMain.requestFocus()

    }
}